<template>
  <div class="order">
    <el-table :data="list" border v-loading="listLoading" style="width: 100%" height="100%">
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="order_id" label="订单编号"></el-table-column>
      <el-table-column prop="user_name" label="用户账号"></el-table-column>
      <el-table-column prop="product_name" label="商品名称"></el-table-column>
      <el-table-column prop="product_num" label="商品数量" width="80"></el-table-column>
      <el-table-column prop="product_price" label="商品价格" width="80"></el-table-column>
      <el-table-column prop="order_time" label="创建时间"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { orderList, delOrder } from "@/api/order";
import { parseTime } from "@/utils/index";
export default {
  data() {
    return {
      listLoading: false,
      list: []
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.listLoading = true;
      orderList().then(res => {
        this.list = res.data.map(item => {
          return {
            ...item,
            order_time: parseTime(item.order_time)
          };
        });
        this.listLoading = false;
      });
    },
    handleDelete(index, data) {
      this.$confirm("此操作将永久删除该图片, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delOrder({ id: data.product_id }).then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.init();
          } else {
            this.$message.error("删除失败");
          }
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.order {
  padding: 20px;
  height: calc(100vh - 80px);
}
</style>